package com.guomei.configs;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * 阿里云OSS配置类
 */
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
@Slf4j
public class AliyunOssConfig {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private String domain;
    private long maxSize;
    private DirConfig dir;

    @Data
    public static class DirConfig {
        private String prefix;
    }

    /**
     * 创建OSS客户端实例
     */
    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * 初始化检查配置
     */
    @PostConstruct
    public void checkConfig() {
        if (endpoint == null || accessKeyId == null || accessKeySecret == null || bucketName == null) {
            log.info("阿里云OSS配置不完整，请检查application.yml");
            throw new RuntimeException("阿里云OSS配置不完整，请检查application.yml");
        }
    }
}
